package cn.boot1112.springboot1112.controller;

import cn.boot1112.springboot1112.entity.User;
import cn.boot1112.springboot1112.mapper.UserMapper;
import cn.boot1112.springboot1112.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 路径：/login
 * 参数：username password
 * 返回：JsonResult
 *      查询用户null：
 *          是：state设置为2
 *          否：用户名正确
 *              判断密码和参数password是否匹配
 *                  是：state 1
 *                  否：state 3
 */
@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("login")
    public JsonResult login(String username,String password){
        //创建JsonResult对象
        JsonResult jsonResult = new JsonResult();
        //根据参数username查询
        User result = userMapper.findByUsername(username);
        //判断结果是否为null
        if(result == null){
            //用户不存在
            jsonResult.setState(2);
        }else {
            //判断密码是否一致
            if(result.getPassword().equals(password)){
                //是，密码正确
                jsonResult.setState(1);
            }else {
                //否，密码错误
                jsonResult.setState(3);
            }
        }


        //返回结果
        return jsonResult;
    }

}
